package server;

import java.rmi.RemoteException;
import java.util.LinkedList;

import client.ClientInterface;

public class ClientManager implements ServerInterface {

	private ConnectionManager connectionManager;
	private Handler handler;

	public ClientManager(ConnectionManager connectionManager, Handler handler,
			TweetParser parser) {
		this.connectionManager = connectionManager;
		this.handler = handler;
	}

	@Override
	public void Connect(String ip, LinkedList<String> tags, String name) throws RemoteException {
		LinkedList<String> modifytag = new LinkedList<String>(tags);
		System.out.println("Some one is connecting");
		System.out.println(tags);
		TweetStack stack = new TweetStack();
		Dispatcher d = new Dispatcher(ip, stack, name);
		new Thread(d).start();
		setHandler(d, tags);
		modifyConnection(modifytag, true);
		DispatcherChecker checker = new DispatcherChecker(d);
		new Thread(checker).start();

	}
		

	public void setHandler(Dispatcher dispatcher, LinkedList<String> tags) {
		DispatcherInfo info = new DispatcherInfo(dispatcher, tags);
		System.out.println("Modifing Handler");
		System.out.println(tags);
		handler.addDispatcher(info);

	}
	
	public void modifyConnection(LinkedList<String> tags, boolean add) {
		System.out.println("Modifing tags");
		System.out.println(tags);
		connectionManager.modifyTags(tags, add);
	}
	
	public void finishTag(String tag){
		LinkedList<String> tags = new LinkedList<String> ();
		tags.add(tag);
		modifyConnection(tags, false);
	}

	/* (non-Javadoc)
	 * @see server.ServerInterface#isAlive()
	 */
	@Override
	public void isAlive() throws RemoteException {		
	}


}
